
/**
 * The Footbar global namespace
 * @class Footbar.Watchdog
 * @static
 */
if (typeof Footbar == "undefined") {
    Footbar = {};
}

Footbar.Watchdog={
    initalTime:2*60000,
    remaining:2*60000, //1 Minute = 60000
    intervalID:-1,
    isPaused:false,
    runUpdate:function(){
        var originalPause=this.isPaused;
        var label=document.getElementById("footbar-watchdog-nextupdate-label");
        //pause the Timer if its enabled
        if(!this.isPaused){
            this.pause();
        }
        label.image="/skin/indicator.gif";
        //Prepare the tree : delete all the node from the treechildren
        var tree=document.getElementById( 'footbar-tree-watchdog' );
        var rootElements=tree.getElementsByTagName("treechildren")[0];
        try {
            while (rootElements.firstChild){
                rootElements.removeChild(rootElements.firstChild);
            };
        } catch(e) {
            alert(e)
        }

        //First need to send request to the server to update now new list
        //Read the SetNames from watchdog-sets-data.xml
        var url="chrome://footbar/content/xml/watchdog-sets-data.xml";
        var httpRequest;
        httpRequest = new XMLHttpRequest();
        httpRequest.overrideMimeType('text/xml');

        httpRequest.open('GET', url, false);
        httpRequest.send(null);

        var xmldata = httpRequest.responseXML;
        var root = xmldata.getElementsByTagName("root")[0];
        var items = root.getElementsByTagName("set");

        for (var i=0;i<items.length;i++){
            label.label="Loading ... ";
            var itemrow=items[i];
            var keywordList=itemrow.getElementsByTagName("pair");
            var keywordsLabel="";
            var kdelim=",";
            for(var k=0;k<keywordList.length;k++){
                if(keywordList.length-1==k)kdelim="";
                keywordsLabel+=keywordList[k].getAttribute("value")+kdelim;
            }
            label.label="Parsing";
            this.addDataToTree('chrome://footbar/content/xml/watchdog-source.xml', itemrow.getAttribute("name"));
        }

        //Resume Timer only if original timer was notPaused
        if(this.isPaused && !originalPause){
            this.pause();
        }else{
            this.pause(true);
        }
    },
/**
 * Add new resource to the tree
 * @param {String} uri XML Resource location
 */
    addDataToTree:function(uri, setname){
        var url=uri;
        var httpRequest;
        httpRequest = new XMLHttpRequest();
        httpRequest.overrideMimeType('text/xml');

        httpRequest.open('GET', url, false);
        httpRequest.send(null);
        var xmldata = httpRequest.responseXML;

        //Add retrived data to the tree
        var tree=document.getElementById( 'footbar-tree-watchdog' );
        var	treechildren = tree.getElementsByTagName("treechildren")[0];
        var root = xmldata.getElementsByTagName("root")[0];
        var items = root.getElementsByTagName("row");

        var label_title= document.createElement("treecell");
        label_title.setAttribute("label",  setname)
        var	treerow = document.createElement("treerow");
        treerow.setAttribute("id",  "setname-treerow-");

        var	treeitem = document.createElement("treeitem");
        treeitem.setAttribute("container", "true");
        treeitem.setAttribute("open", "true");


        var  sub_treechildren = document.createElement("treechildren");


        for (var i=0;i<items.length;i++){
            var itemrow=items[i];
            var title=itemrow.getElementsByTagName("title")[0];
            var keywords=itemrow.getElementsByTagName("keywords")[0];
            var description=itemrow.getElementsByTagName("description")[0];
            var code=itemrow.getElementsByTagName("code")[0];

            var dataObject;
            dataObject={
                label:title.textContent,
                key:"",
                properties:"unread"
            };
            sub_treechildren.appendChild( Footbar.TreeUtil.cellNode(dataObject) );
        }

        treerow.appendChild(label_title);
        treeitem.appendChild(treerow);
        treeitem.appendChild(sub_treechildren);
        treechildren.appendChild(treeitem);

    },

/**
 * Initailize the Timer
 */
    initTimer:function(){
        this.intervalID = setInterval(function(){
            var label=document.getElementById("footbar-watchdog-nextupdate-label");

            var minutes=Math.floor(Footbar.Watchdog.remaining/(60000));
            var seconds=Math.floor((Footbar.Watchdog.remaining%60000)/1000);
            label.label=" Next update in   "+minutes+" : "+seconds+"  ";

            Footbar.Watchdog.remaining-=1000;

            if(minutes<=0 && seconds<=0){
                Footbar.Watchdog.remaining=Footbar.Watchdog.initalTime;
                Footbar.Watchdog.runUpdate();
            }
        }, 1000);
    },
/**
 * Pause/unpause the update manager
 */
    pause:function(forcePause){
        var label=document.getElementById("footbar-watchdog-nextupdate-label");
        if(!this.isPaused || forcePause){
            clearInterval(this.intervalID);
            label.image="/skin/pause.png"
            label.label="Updates Paused";
            this.isPaused=true;
        }else{
            this.isPaused=false;
            label.image="/skin/timestamp.png";
            label.label="Resuming";
            Footbar.Watchdog.initTimer();
        }
    }
} 	 	
